package com.joelbitar.switchctrl;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

public class serverdetails extends Activity {
	
	public static final int MENU_SAVE = 0;
	public static final int MENU_TEST = 0;
	public static final String INTENT_ADD = "intent_add";
	public static final String INTENT_EDIT = "intent_edit";
	public static final String INTENT_EDIT_SERVER_ID = "intent_edit_server_id";
	
	private boolean mode_add_server = false;
	private boolean mode_edit_server = false;
	private int edit_server_id;
	
	@Override
	public void onCreate(Bundle icicle) {	
		
		Log.v("activity", "Server details");
		
        super.onCreate(icicle);
        setContentView(R.layout.settings_servers_server);

        try
        {
        	Object addObject = getIntent().getExtras().get(INTENT_ADD);
        	
        	if(addObject.equals(true))
        	{
        		mode_add_server = true;
        	}
        }
        catch(Exception e)
        {
        	Log.i("intentadd", "NOT adding");
        	e.printStackTrace();
        }
        
        mode_edit_server = false;
        
        try
        {
        	Object addObject = getIntent().getExtras().get(INTENT_EDIT);
        	
        	if(addObject.equals(true))
        	{
        		mode_edit_server = true;

            	Object editObject = getIntent().getExtras().get(INTENT_EDIT_SERVER_ID);
            	
            	edit_server_id = Integer.parseInt(editObject.toString());
        	}
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        }

        Log.i("goingtoadd", "" +  mode_add_server);

        Log.i("goingtoedit", "" +  mode_edit_server);
        
        if(mode_edit_server)
        {
            Log.i("goingtoedit", "" +  edit_server_id);
        	populateServerDetails();
        }
        
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		/*
		if(this.mode_add_server)
		{
			menu.add(0, MENU_SAVE, 0, getString(R.string.menu_settings_server_add)).setIcon(android.R.drawable.ic_menu_add);
		}
		else
		{
			// We are in edit mode
			menu.add(0, MENU_SAVE, 0, getString(R.string.menu_settings_server_save)).setIcon(android.R.drawable.ic_menu_save);
		}
		
		// Test button
		menu.add(0, MENU_TEST, 0, getString(R.string.menu_settings_server_test)).setIcon(android.R.drawable.ic_menu_rotate);
		*/
		return true;
	}
	
	public void populateServerDetails() {
		ServerDbAdapter serveradapter = new ServerDbAdapter(this);

		Server server = serveradapter.getServer(this.edit_server_id);
		
		Log.w("populateserverdetails", "Populating server details");
		
		Log.i("name", server.getName());
		
		EditText nameField = (EditText) findViewById(R.id.name);
		EditText urlField = (EditText) findViewById(R.id.url);
		EditText usernameField = (EditText) findViewById(R.id.username);
		EditText passwordField = (EditText) findViewById(R.id.password);
		CheckBox visible = (CheckBox) findViewById(R.id.show);
		
		nameField.setText(server.getName());
		urlField.setText(server.getHosturl());
		usernameField.setText(server.getUsername());
		passwordField.setText(server.getPassword());
		visible.setChecked(server.getVisible());
	}
	public Server getServerFromFormData() {
		Server server = new Server();

		EditText nameField = (EditText) findViewById(R.id.name);
		EditText urlField = (EditText) findViewById(R.id.url);
		EditText usernameField = (EditText) findViewById(R.id.username);
		EditText passwordField = (EditText) findViewById(R.id.password);
		CheckBox visible = (CheckBox) findViewById(R.id.show);

		Editable name = (Editable) nameField.getText();
		Editable url = (Editable) urlField.getText();
		Editable username = (Editable) usernameField.getText();
		Editable password = (Editable) passwordField.getText();
		
		Log.v("serverdetails", "Data that user input");
		Log.i("name", name.toString());
		Log.i("url", url.toString());
		Log.i("username", username.toString());
		Log.i("password", password.toString());
		
		if(visible.isChecked()){
			Log.i("show", "Yepp");
		}
		else
		{
			Log.i("show", "Noep");
		}
		
		server.setName(name.toString());
		server.setHosturl(url.toString());
		server.setUsername(username.toString());
		server.setPassword(password.toString());
		server.setVisible(visible.isChecked());
		
		return server;
	}
	public boolean saveServer(View view) {
		
		ServerDbAdapter serveradapter = new ServerDbAdapter(this);
		
		Server server = this.getServerFromFormData();
		
		if(this.mode_add_server)
		{
			serveradapter.insertServer(server);
		}
		
		if(this.mode_edit_server)
		{
			server.setId(this.edit_server_id);
			
			//serveradapter.insertServer(server);
			serveradapter.updateServer(server);
		}
		
		Intent resultIntent = new Intent();
		setResult(Activity.RESULT_OK, resultIntent);
		resultIntent.putExtra(settings.PUBLIC_STATIC_STRING_IDENTIFIER, 1);
		finish();
		
		return true;
	}
}
